package com.doughit.music163.config;


import com.doughit.music163.constant.Constants;
import com.doughit.music163.constant.IncreaseType;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.stream.Collectors;

public class Config {
    public static String COOKIES = Constants.DEFAULT_COOKIES_PATH;
    public static IncreaseType TYPE = IncreaseType.TIMES;
    public static List<String> PLAY_LIST_IDS = Constants.DEFAULT_PLAYLIST_IDS;
    public static List<String> SONG_IDS = Constants.DEFAULT_SONG_IDS;
    public static String DRIVER = Constants.CHROME_DRIVER_PATH;

    public static Integer SONG_INTERVAL = Constants.DEFAULT_SONG_INTERVAL;
    public static Integer LIST_INTERVAL = Constants.DEFAULT_LIST_INTERVAL;
    public static Integer RECYCLE_LIST_INTERVAL = Constants.DEFAULT_RECYCLE_LIST_INTERVAL;
    private static Properties properties = new Properties();
    public final static String CONFIG_FILE = "config.properties";
    private final static String COMMENTS = """
             cookies: The path of cookies' file, which is a json file copied from chrome by editthiscookie.crx
             type: times, the number of song playlists played; or total, the number of songs played; or song, the songs played
             play_list_ids: the playlist id numbers when type equals to times, e.g., 465602586,732091305,876203728,807001518
             song_ids: the song id numbers when type equals to song, e.g., 512376326,863706084
            """;

    /**
     * 保存配置
     *
     * @throws IOException
     */
    public static void save() throws IOException {
        properties.setProperty("cookies_path", COOKIES);
        properties.setProperty("type", TYPE.name());
        properties.setProperty("play_list_ids", StringUtils.join(PLAY_LIST_IDS, ","));
        properties.setProperty("song_ids", StringUtils.join(SONG_IDS, ","));
        properties.setProperty("driver", DRIVER);
        properties.setProperty("song_interval", SONG_INTERVAL.toString());
        properties.setProperty("list_interval", LIST_INTERVAL.toString());
        properties.setProperty("recycle_list_interval", RECYCLE_LIST_INTERVAL.toString());
        properties.store(new FileWriter(CONFIG_FILE), COMMENTS);
    }

    /**
     * 加载配置
     */
    public static void load() {
        try {
            if (new File(CONFIG_FILE).exists()) {
                properties.load(new FileReader(CONFIG_FILE));
                COOKIES = properties.getProperty("cookies_path", Constants.DEFAULT_COOKIES_PATH);
                TYPE = IncreaseType.valueOf(properties.getProperty("type", IncreaseType.TIMES.name()).toUpperCase(Locale.ROOT));
                DRIVER = properties.getProperty(DRIVER, Constants.CHROME_DRIVER_PATH);
                String ids = properties.getProperty("play_list_ids","");
                if (ids.trim().equals("")) {
                    PLAY_LIST_IDS = Constants.DEFAULT_PLAYLIST_IDS;
                } else {
                    PLAY_LIST_IDS = Arrays.stream(ids.split(","))
                            .map(String::trim)
                            .collect(Collectors.toList());
                }
                String songIds = properties.getProperty("song_ids","");
                if (songIds.trim().equals("")) {
                    SONG_IDS = Constants.DEFAULT_SONG_IDS;
                } else {
                    SONG_IDS = Arrays.stream(songIds.split(","))
                            .map(String::trim)
                            .collect(Collectors.toList());
                }
                SONG_INTERVAL = Integer.valueOf(properties.getProperty("song_interval", String.valueOf(Constants.DEFAULT_SONG_INTERVAL)));
                LIST_INTERVAL = Integer.valueOf(properties.getProperty("list_interval", String.valueOf(Constants.DEFAULT_LIST_INTERVAL)));
                RECYCLE_LIST_INTERVAL = Integer.valueOf(properties.getProperty("recycle_list_interval", String.valueOf(Constants.DEFAULT_RECYCLE_LIST_INTERVAL)));
            } else {
                save();
                System.exit(0);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

}
